package com.share.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

@Slf4j
public class MavenUtil {
    public static String getMavenRepository() {
        return getMavenRepository(null);
    }

    public static String getMavenRepository(String include) {
        String localRepository = null;
        if (!StringUtils.isBlank(EnvUtil.MAVEN_HOME)) {
            String fileDir = EnvUtil.MAVEN_HOME + File.separator + "conf";
            List<File> settings = FileUtil.listFile(fileDir, include, ".xml");
            for (File file : settings) {
                String read = FileUtil.read(file);
                try {
                    Map<String, String> xmlMap = XmlUtil.xml2Map(read);
                    localRepository = xmlMap.get("localRepository");
                    if (StringUtils.isNotBlank(localRepository) && !ArrayUtils.isEmpty(new File(localRepository).listFiles())) {
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return localRepository;
    }

    public static void main(String[] args) {
        String mavenRepository = getMavenRepository();
        System.out.println(mavenRepository);
    }
}
